//两两交换链表中的节点
/*给你一个链表，两两交换其中相邻的节点，并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题（即，只能进行节点交换）。*/
// 循环迭代
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* newhead = new ListNode(-1, head);
        ListNode* prev = newhead;
        ListNode* cur;
        ListNode* n;
        ListNode* nn;
        while (prev->next && prev->next->next) {
            cur = prev->next;
            n = cur->next;
            nn = n->next;
            prev->next = n;
            cur->next = nn;
            n->next = cur;
            prev = cur;
        }
        return newhead->next;
    }
};


//递归
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return head;
        ListNode* n = head->next;
        ListNode* nn = head->next->next;
        head->next = swapPairs(nn);
        n->next = head;
        return n;
    }
};
//LCR 026. 重排链表
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode* newhead1 = new ListNode(-1);
        ListNode* newhead2 = new ListNode(-1);
        ListNode *f = head, *s = head;
        ListNode* tail1 = newhead1;
        // ListNode* tail2 = newhead2;
        while (f && f->next) {
            // ListNode* temp = s;
            tail1->next = s;
            tail1 = tail1->next;
            s = s->next;
            // temp->next = nullptr;
            if (f)
                f = f->next;
            if (f)
                f = f->next;
        }
        tail1->next = nullptr;
        while (s) {
            ListNode* temp = s;
            s = s->next;
            temp->next = newhead2->next;
            newhead2->next = temp;
        }
        bool floag = true;
        ListNode* cur1 = newhead1->next;
        ListNode* cur2 = newhead2->next;
        ListNode* newhead = new ListNode(-1);
        ListNode* tail = newhead;
        while (cur1 && cur2) {
            if (floag) {
                floag = false;
                // ListNode* temp = cur1;
                tail->next = cur1;
                tail = tail->next;
                cur1 = cur1->next;
                // temp->next = nullptr;
            } else {
                floag = true;
                // ListNode* temp = cur2;
                tail->next = cur2;
                tail = tail->next;
                cur2 = cur2->next;
                // temp->next = nullptr;
            }
        }
        if (cur1) {
            tail->next = cur1;
        }
        if (cur2) {
            tail->next = cur2;
        }
        // head = newhead->next;*/
    }
};
